
const {verifyToken} = require('../common/token')

// 错误中间件
function middleError(err, req, res, next) {
    switch (err) {
        case 5:
            res.send({
                code: err,
                message: '参数错误'
            })
            break;
        case 401:
            res.send({
                code: err,
                message: '请重新登录'
            })
            break;

        default:
            break;
    }
}



// 验证token 

async function  checkToken(req,res,next){
    const {authorization} = req.headers

    if(authorization !=null){
        // 解密 
            let result = await verifyToken(authorization)
            // 判断解密成功没有
            if(result.code == 0){
                  // 解密失败
                return  res.send(result)
            }
            next()
    }else{
        next(401)
    }
}

module.exports = {
    middleError,checkToken
}